package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.entity.ClassRoom;
import nyist.media.enums.RoomType;
import nyist.media.page.ApiResult;
import nyist.media.service.ApplyRecordService;
import nyist.media.service.ClassRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 教室模块
 *
 * @author : dcf
 * @date : 2019/3/15
 */
@RestController
@RequestMapping(value = "/admin/classroom", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public class ClassRoomController extends BaseController {

    @Autowired
    private ClassRoomService classRoomService;

    /**
     * 查看教室列表
     *
     * @return 教室列表
     */
    @GetMapping
    public Map<String, Object> classRooms(String keyword, Integer page, Integer limit, RoomType type) {
        return toResultPage(classRoomService.findPage(keyword, type, new PageRequest(page - 1, limit)));
    }

    @Log(value = "新增教室")
    @PostMapping
    public Map<String, Object> save(ClassRoom classRoom) {
        classRoomService.save(classRoom, getCurrentInfo());
        return ApiResult.SUCCESS.getMap();
    }

    @Log(value = "编辑教室")
    @PutMapping("/{id}")
    public Map<String, Object> edit(@PathVariable Long id, ClassRoom classRoom) {
        classRoom.setId(id);
        classRoomService.save(classRoom, getCurrentInfo());
        return ApiResult.SUCCESS.getMap();
    }

    @Log(value = "回收教室")
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        classRoomService.deleteRoom(id);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 查看教室详情
     */
    @GetMapping("/{id}")
    public Map<String, Object> viewDetail(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("result", classRoomService.findDetail(id));
    }

    /**
     * 申请教室
     * @param id 教室ID
     * @return 状态码 20000 状态信息：响应成功
     */
    @PostMapping("/{id}/apply")
    public Map<String, Object> applyClassRoom(@PathVariable Long id) {
        classRoomService.apply(id, getCurrentInfo());
        return ApiResult.SUCCESS.getMap();
    }

    /** 归还教室*/
    @PutMapping("/{id}/return")
    public Map<String, Object> returnRoom(@PathVariable Long id) {
        classRoomService.returnRoom(id);
        return ApiResult.SUCCESS.getMap();
    }

}

